<?php
/*public class Node {
    public int val;
    public List<Node> children;

    public Node() {
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}*/


// 层序
/*public List<List<Integer>> levelOrder(Node root) {
    List<List<Integer>> ans = new ArrayList<>();
    robot(root, ans, 0);
    return ans;
}

private void robot(Node root, List<List<Integer>> ans, int level) {
    if (root == null) {
        return;
    }

    if (ans.size() <= level) {
        ans.add(new ArrayList<>());
    }

    ans.get(level).add(root.val);

    if (root.children == null) {
        return;
    }
    for (Node child : root.children) {
        robot(child, ans, level + 1);
    }
}*/

/*class Solution {
    public $level = 0;
    public $values = [];
    function levelOrder($root) {
        if(is_null($root)){
            return $this->values;
        }
        if(!isset($this->values[$this->level])){
            $this->values[$this->level] = [];
        }
        $this->values[$this->level][] = $root->val;
        foreach($root->children as $child){
            $this->level++;
            $this->levelOrder($child);
            $this->level--;
        }

        return $this->values;
    }
}*/


// 前序
/*public List<Integer> preorder(Node root) {
    List<Integer> ans = new ArrayList<>();
    robot(root, ans);
    return ans;
}

private void robot(Node root, List<Integer> ans) {
    if (root == null) {
        return;
    }

    ans.add(root.val);
    if (root.children == null) {
        return;
    }

    for (Node child : root.children) {
        robot(child, ans);
    }
}*/


// 后序
/*public List<Integer> postorder(Node root) {
    List<Integer> ans = new ArrayList<>();
    robot(root, ans);
    return ans;
}

private void robot(Node root, List<Integer> ans) {
    if (root == null) {
        return;
    }

    if (root.children != null) {
        for (Node child : root.children) {
            robot(child, ans);
        }
    }
    ans.add(root.val);
}*/

/*class Solution {
    public $val = [];
    function postorder($root) {
        if(!$root){
            return $this->val;
        }
        foreach($root->children as $child){
            $this->postorder($child);
        }
        $this->val[] = $root->val;
        return $this->val;
    }
}*/